function downloadUrlClk() {
    let nodes = zTree.getSelectedNodes();
    if (nodes.length > 0) {
        let url = nodes[0].id
        let link = document.createElement("a");
        link.download = nodes[0].name;
        link.href = "/gofile/download?file=" + url;
        link.click();

        // To delete the link element, we can use the following code:
        link.remove();
    }
    hideRMenu()
}

function createDirClk() {
    $("#createDirDlg").css("display", "flex")
}

function closeCreateDirDlg() {
    $("#createDirDlg").css("display", "none")
}

function createDirOKClk() {
    let nodes = zTree.getSelectedNodes();
    let parentDir = "";
    let refreshNode = null;
    let distDir = ""
    let dirName = $("#dirName").val()
    if (nodes.length > 0) {
        if (nodes[0].isParent) {
            parentDir = nodes[0].id+"/"
            refreshNode = nodes[0]
        } else {
            let parentNode = nodes[0].getParentNode()
            if (parentNode) {
                parentDir = parentNode.id+"/"
                refreshNode = parentNode
            } else {
                parentDir = ""+"/"
            }
        }
        distDir = parentDir + dirName
    }else{
        distDir = dirName
    }
    $.ajax({
        url: "/gofile/createDir",
        contentType: "application/json",
        dataType: "json",
        data: { file: distDir },
        success: function (data) {
            if (data.success) {
                zTree.reAsyncChildNodes(refreshNode, "refresh");
                closeCreateDirDlg()
            } else {
                alert("创建失败")
            }
        }

    })
}

function delUrlClk() {
    let nodes = zTree.getSelectedNodes();
    if (nodes.length > 0) {
        $.ajax({
            url: "/gofile/del",
            contentType: "application/json",
            dataType: "json",
            data: { file: nodes[0].id },
            success: function (data) {
                if (data.success) {
                    alert("删除成功")
                    let parentNode = nodes[0].getParentNode()
                    if (parentNode) {
                        zTree.reAsyncChildNodes(parentNode, "refresh");
                    } else {
                        //loadData("")
                        zTree.reAsyncChildNodes(null, "refresh")
                    }
                } else {
                    alert("删除失败")
                }
            }

        })
    }
}

function uploadBtnClk() {
    let ff = $("#ff");
    let file = ff.find("input[name='file']");
    if (file.length === 0) {
        alert("请选文件")
        return
    }
    if (file[0].files.length === 0) {
        alert("请选文件")
        return
    }
    let name = file[0].files[0].name
    ff.find("input[name='name']").val(name)

    let nodes = zTree.getSelectedNodes();
    let needNode = null
    if (nodes.length > 0) {
        let node = nodes[0]
        if (node.isParent) {
            needNode = node
            ff.find("input[name='path']").val(node.id)
        } else {
            needNode = parentNode
            let parentNode = node.getParentNode()
            if (parentNode) {
                ff.find("input[name='path']").val(parentNode.id)
            } else {
                ff.find("input[name='path']").val("")
            }
        }
    } else {
        ff.find("input[name='path']").val("")
    }
    $.ajax({
        url: "/gofile/upload",
        type: 'POST',
        cache: false,
        data: new FormData(ff[0]),
        processData: false,
        contentType: false,
        dataType: "json",
        success: function (data) {
            if (data.success) {
                alert("上传成功")
                if (needNode) {
                    zTree.reAsyncChildNodes(needNode, "refresh");
                } else {
                    //loadData("")
                    //zTree.refresh()
                    zTree.reAsyncChildNodes(null, "refresh")
                }
            } else {
                alert("上传失败")
            }
        }
    });
}

function copyUrlClk() {
    let nodes = zTree.getSelectedNodes();
    if (nodes.length > 0) {
        alert(nodes[0].downloadUrl)
    }
    hideRMenu()
}

function convertSize(size) {
    if (size < 1024) {
        return size + "B";
    } else if (size >= 1024 && size < 1024 * 1024) {
        return size / 1024 + "KB";
    } else if (size >= 1024 * 1024 && size < 1024 * 1024 * 1024) {
        return size / (1024 * 1024) + "MB";
    } else if (size >= 1024 * 1024 * 1024) {
        return size / (1024 * 1024 * 1024) + "GB";
    }
}


function showRMenu(type, x, y) {
    $("#rMenu ul").show();
    if (type == "root") {
        $("#m_del").hide();
        $("#m_check").hide();
        $("#m_unCheck").hide();
    } else {
        $("#m_del").show();
        $("#m_check").show();
        $("#m_unCheck").show();
    }

    y += document.body.scrollTop;
    x += document.body.scrollLeft;
    rMenu.css({ "top": y + "px", "left": x + "px", "visibility": "visible" });

    $("body").bind("mousedown", onBodyMouseDown);
}

function hideRMenu() {
    if (rMenu) rMenu.css({ "visibility": "hidden" });
    $("body").unbind("mousedown", onBodyMouseDown);
}

function onBodyMouseDown(event) {
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
        rMenu.css({ "visibility": "hidden" });
    }
}
